<?php

class RedisAPI {

    private $Redis = null;

    /**
     * 构造方法, 在这里连接Redis库
     */
    public function __construct()
    {
        $this->Redis = new \Redis();
        $this->Redis->connect(C('REDIS_HOST'), C('REDIS_PORT'), C('REDIS_TIMEOUT'));
        $this->Redis->auth(C('REDIS_PASSWORD'));
        $this->Redis->select(C('REDIS_BASE_DB_INDEX'));     // 选择具体的Redis库
    }


    /** *************************************************************************** */
    /**                           BASE  && STRING                                   */
    /** *************************************************************************** */

    public function set( $key, $value, $timeout = 0 )
    {
        return $this->Redis->set($key, $value, $timeout);
    }

    public function get( $key )
    {
        return $this->Redis->get($key);
    }

    public function del( $key1, $key2 = null, $key3 = null )
    {
        return $this->Redis->del($key1, $key2, $key3);
    }

    public function exists( $key )
    {
        return $this->Redis->exists($key);
    }

    /** *************************************************************************** */
    /**                                  LIST                                       */
    /** *************************************************************************** */

    public function lPush( $key, $value1, $value2 = null, $valueN = null )
    {
        return $this->Redis->lPush($key, $value1, $value2, $valueN);
    }

    public function rPush( $key, $value1, $value2 = null, $valueN = null )
    {
        return $this->Redis->rPush($key, $value1, $value2, $valueN);
    }

    public function lPushx( $key, $value )
    {
        return $this->Redis->lPushx($key, $value);
    }

    public function rPushx( $key, $value )
    {
        return $this->Redis->rPushx($key, $value);
    }

    public function lPop( $key )
    {
        return $this->Redis->lPop($key);
    }

    public function rPop( $key )
    {
        return $this->Redis->rPop($key);
    }

    public function blPop( array $keys )
    {
        return $this->Redis->blPop($keys);
    }

    public function brPop( array $keys )
    {
        return $this->Redis->brPop($keys);
    }

    public function lLen( $key )
    {
        return $this->Redis->lLen($key);
    }

    public function lSize( $key )
    {
        return $this->Redis->lLen($key);
    }

    public function lIndex( $key, $index )
    {
        return $this->Redis->lIndex($key, $index);
    }

    public function lGet( $key, $index )
    {
        return $this->Redis->lGet($key, $index);
    }

    public function lSet( $key, $index, $value )
    {
        return $this->Redis->lSet($key, $index, $value);
    }

    public function lRange( $key, $start, $end )
    {
        return $this->Redis->lRange($key, $start, $end);
    }

    public function lGetRange( $key, $start, $end )
    {
        return $this->Redis->lGetRange($key, $start, $end);
    }

    public function lTrim( $key, $start, $stop )
    {
        return $this->Redis->lTrim($key, $start, $stop);
    }

    public function listTrim( $key, $start, $stop )
    {
        return $this->Redis->listTrim($key, $start, $stop);
    }

    public function lRem( $key, $value, $count )
    {
        return $this->Redis->lRem($key, $value, $count);
    }

    public function lRemove( $key, $value, $count )
    {
        return $this->Redis->lRemove($key, $value, $count);
    }

    public function lInsert( $key, $position, $pivot, $value )
    {
        return $this->Redis->lInsert($key, $position, $pivot, $value);
    }

    public function keys( $pattern )
    {
        return $this->Redis->keys($pattern);
    }

    public function delete( $key1, $key2 = null, $key3 = null )
    {
        $this->Redis->delete($key1, $key2, $key3);
    }
    public function publish( $channel, $message )
    {
        $this->Redis->publish( $channel, $message );
    }
    public function subscribe( $channels, $callback )
    {
        $this->Redis->subscribe( $channels, $callback );
    }
}